//
//  RMMHomeViewController.m
//  Romomo
//
//  Created by puman on 16/9/30.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMHomeViewController.h"
#import "RMMChooseRegionViewController.h"
#import <MapKit/MapKit.h>
#import "RMMGuangGaoView.h"
#import "RMMHomeTableViewCell.h"
#import "RMMScrollView.h"
#import "RMMMySkillTableViewCell.h"
#import "RMMGoodsDetialsViewController.h"
#import <objc/runtime.h>
#import "RMMLongingViewController.h"
#import "RMMNavigationViewController.h"
#import "RMMTaoCanDetailsViewController.h"
#import "RMMSkillDeltailsViewController.h"
#import "RMMOderAdressViewController.h"
#import "UINavigationBar+RMMBackGroundColor.h"
#import "RMMCommonBut.h"
#import "RMMNoSkillTableViewCell.h"
#import "RMMOderAdressViewController.h"

#define NAVBAR_CHANGE_POINT 50

@interface RMMHomeViewController ()<SDCycleScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@end

@implementation RMMHomeViewController
{
    SDCycleScrollView *_sdCycleSrollView;
    UIView *_headNormalView;
    RMMGuangGaoView *_guangGaoView;
    RMMScrollView *_scrollView;
    RMMCommonBut *_titltBut;
    RMMNotNetWorkView *_networkView;
    UITableView *_tableView;
    NSArray *_prodTypeArray;
    NSMutableDictionary *_dataDic;
    NSArray *_skillArray;
    NSTimer *_timer;
    UIButton *_cityBut;
    NSString *_locationCity;
    NSString *_currentCity;
    NSString *_region_id;
    NSInteger _number;
    NSInteger _buttonTag;
    NSInteger _page;
    NSString *_fuWuType;
    NSString *_pageindex;
    BOOL _isLoginSuccess;
    UIImageView *_backImageView;
    RMMCommonBut *_but;
}
- (void)dealloc{
    NS_REM(@"TUICHUDENGLU", nil);
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_buttonTag == 10 + _prodTypeArray.count - 1) {
        if (_isLoginSuccess != [RMMFileManager shareHandle].isLoginSuccess) {
            [self getMySkillData];
        }
    }
    [[RMMFileManager shareHandle].bookingDic removeAllObjects];
    [RMMFileManager shareHandle].fromSkillType = @"0";
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_timer invalidate];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self addViwes];
    self.navigationController.navigationBar.translucent = NO;
    _isLoginSuccess = [RMMFileManager shareHandle].isLoginSuccess;
    //[self guangGao];
   __weak RMMHomeViewController *mySelf = self;
    [mySelf addRefresh];
    NS_ADD(tuiChuDengLu, @"TUICHUDENGLU", nil);
    _titltBut = [[RMMCommonBut alloc] initWithFrame:CGRectMake(0, 0, 150, 30) title:@"未选择"];
    BUTTON_S(_titltBut, chooseRegion);
    self.navigationItem.titleView = _titltBut;
    [self addLeftItem];
    [self presentViewController];
}
- (void)addLeftItem{
    _but = [[RMMCommonBut alloc] initWithFrame:CGRectMake(0, 0, 80, 30) title:@"上门"];
    BUTTON_S(_but, rightItemClick);
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:_but];
    self.navigationItem.leftBarButtonItem = item;
}
- (void)rightItemClick{
    _buttonTag = 10;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"服务类型" message:@"请选择你想要的服务类型" preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"上门服务" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [RMMFileManager shareHandle].fuWuType = @"1";
        _but.nameLabel.text = @"上门";
        UIButton *button = [self.view viewWithTag:10];
        [self scrollViewButClick:button];
        NS_POST(@"SHOPFRESH", nil, nil);
        NS_POST(@"ORDERUPDATA", nil, nil);
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"到店服务" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [RMMFileManager shareHandle].fuWuType = @"0";
        UIButton *button = [self.view viewWithTag:10];
        [self scrollViewButClick:button];
        _but.nameLabel.text = @"到店";
        NS_POST(@"SHOPFRESH", nil, nil);
        NS_POST(@"ORDERUPDATA", nil, nil);
    }];
    UIAlertAction *actionC = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
    }];
    if (DEVICE >= 9.0) {
        if ([[RMMFileManager shareHandle].fuWuType isEqualToString:@"1"]) {
            [actionA setValue:COLORFROMCODE(0x9B9B9B, 1) forKey:@"titleTextColor"];
        } else {
            [actionB setValue:COLORFROMCODE(0x9B9B9B, 1) forKey:@"titleTextColor"];
        }
    }
    /*
     [callAction setValue:kBasicColor forKey:@"titleTextColor"];
     [tiXianAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
     [shuoMingAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
     [cancel setValue:COLORFROMCODE(0x9B9B9B, 1) forKey:@"titleTextColor"];
     */
    [alertController addAction:actionA];
    [alertController addAction:actionB];
    [alertController addAction:actionC];
    [self presentViewController:alertController animated:YES completion:nil];
}
// 退出登录
- (void)tuiChuDengLu{
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    _buttonTag = 10;
    for (int i = 0; i < _prodTypeArray.count; i++) {
        UIButton *buttons = [_scrollView viewWithTag:10 + i];
        if (buttons.tag == _buttonTag) {
            [buttons setTitleColor:COLORFROMCODE(0x2C2C2C, 1) forState:(UIControlStateNormal)];
            buttons.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
            buttons.userInteractionEnabled = NO;
        } else {
            [buttons setTitleColor:COLORFROMCODE(0xBFBFBF, 1) forState:(UIControlStateNormal)];
            buttons.backgroundColor = kWhiteColor;
            buttons.userInteractionEnabled = YES;
        }
    }
    _skillArray = nil;
    [_tableView reloadData];
}
- (void)addRefresh{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getScrollPhoto];
        if(_buttonTag == 10 + _prodTypeArray.count - 1) {
            if ([RMMFileManager shareHandle].isLoginSuccess) {
                [self getMySkillData];
            } else {
                [self addLoginAlertView];
            }
        } else {
            if (!_prodTypeArray.count) {
                [self getFuWuType];
            } else {
                [self getRequestDataWith:_fuWuType forPage:@"1"];
            }
        }
    }];
}
// 获得服务类别
- (void)getFuWuType{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    NSString *url = FuWuTypeUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            [_tableView.mj_header endRefreshing];
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        [RMMFileManager shareHandle].fuWuTypeArray = [NSMutableArray array];
        if ([status isEqualToString:@"1"]) {
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dic in dataDic[@"data"]) {
                [array addObject:[RMMFileManager setValueForDic:dic]];
                [[RMMFileManager shareHandle].fuWuTypeArray addObject:dic];
            }
            if (array.count == 0) {
                return ;
            }
            NSDictionary *dic2 = @{@"CateId":@"",@"CateName":@"特惠套餐"};
            NSDictionary *dic3 = @{@"CateId":@"",@"CateName":@"我的技师"};
            [array insertObject:dic2 atIndex:0];
            [array addObject:dic3];
            _prodTypeArray = array;
            [self addFuWuTypeScrollViewWith:_prodTypeArray];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } error:^(NSError *errors) {
        [_tableView.mj_header endRefreshing];
    } view:self.view];
}
// 轮播图图片
- (void)getScrollPhoto{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"region_id"] = _region_id;
    NSString *url = GetBannerUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }
        
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSArray *aray = dataDic[@"data"];
            NSMutableArray *imageArr = [NSMutableArray array];
            for (NSDictionary *dic in aray) {
                [imageArr addObject:[NSString stringWithFormat:@"%@%@",kBaseImageUrl,dic[@"FILE_URL"]]];
            }
            [_sdCycleSrollView setImageURLStringsGroup:imageArr];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
    } view:self.view];

}
-  (void)getRequestDataWith:(NSString *)type forPage:(NSString *)page{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"prod_type"] = [RMMFileManager shareHandle].fuWuType;
    parDic[@"region_id"] = _region_id;
    NSString *url = nil;
    if (_buttonTag == 10) {
        url = HomeTaoCanDataUrl;
    }  else {
        parDic[@"pageindex"] = page;
        parDic[@"type"] = type;
        url = HomeOneClassDataUrl;
    }
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }

    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSArray *array = dataDic[@"data"];
            if (!array.count) {
                if([[RMMFileManager shareHandle].fuWuType isEqualToString:@"0"]){
                    if (_buttonTag == 10) {
                        _backImageView.hidden = NO;
                    }
                }
            } else {
                _backImageView.hidden = YES;
            }
            NSDictionary *keyDic = _prodTypeArray[_buttonTag - 10];
            NSString *key = keyDic[@"CateName"];
            NSMutableArray *value = _dataDic[key];
            if (value == nil) {
                value = [NSMutableArray array];
                [_dataDic setObject:value forKey:key];
            }
            [value removeAllObjects];
            for (NSDictionary *dic in array) {
                RMMHomeGoodsModel *model = [[RMMHomeGoodsModel alloc] init];
                [model setValuesForKeysWithDictionary:[RMMFileManager setValueForDic:dic]];
                if ([[NSString stringWithFormat:@"%@",model.TIME_LEN] isEqualToString:@"0"]) {
                }
                [value addObject:model];
            }
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } error:^(NSError *errors) {
        [_tableView.mj_header endRefreshing];
    } view:self.view];
}
// 区域选择
- (void)presentViewController{
    RMMChooseRegionViewController *VC = [[RMMChooseRegionViewController alloc] init];
    VC.currentCity = ^void(NSString *cityName, NSString *cityCode) {
        _region_id = cityCode;
        [RMMFileManager shareHandle].region_id = cityCode;
        _currentCity = cityName;
        _titltBut.nameLabel.text = cityName;
        _buttonTag = 10;
        [self getScrollPhoto];
        [self setButton:_cityBut withTitle:cityName];
        [self getFuWuType];
//        if (_buttonTag == 10 + _prodTypeArray.count - 1) {
//            if ([RMMFileManager shareHandle].isLoginSuccess) {
//                [self getMySkillData];
//            } else {
//                [self addLoginAlertView];
//            }
//        } else {
//           // [self getRequestDataWith:_fuWuType forPage:@"1"];
//        }
    };
    [self presentViewController:VC animated:YES completion:nil];
}
// 轮播图
- (void)addHeadScrollView{
    _headNormalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 200)];
    _headNormalView.backgroundColor = kWhiteColor;
    NSArray *imageAarray = @[@"",@"",@"",@""];
    _sdCycleSrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0 , kScreenWideth, 200) shouldInfiniteLoop:YES imageNamesGroup:imageAarray];
    // 公告
    _sdCycleSrollView.delegate = self;
    _sdCycleSrollView.backgroundColor = [UIColor brownColor];
    _sdCycleSrollView.pageDotColor = RGBA(191, 191, 191, 191);
    _sdCycleSrollView.currentPageDotColor = RGBA(74, 74, 74, 1);
    _sdCycleSrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    _sdCycleSrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 轮播方向
    _sdCycleSrollView.autoScrollTimeInterval = 3.0; // 间隔的秒数
    [_headNormalView addSubview:_sdCycleSrollView];
    
    _tableView.tableHeaderView = _headNormalView;
}
// 类别的scrollView
- (void)addFuWuTypeScrollViewWith:(NSArray *)array{
    //_scrollView.contentSize = CGSizeMake(array.count * 80 + 30, 54);
    [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, array.count * 80 + 30, 0.5)];
    lineLabel.backgroundColor = RGBA(221, 221, 221, 1);
    [_scrollView addSubview:lineLabel];
    
    CGFloat width = 0;
    CGFloat maric = 0;
    if (kScreenWideth == 320) {
        width = 70;
    } else {
        width = 80;
    }
    maric = (kScreenWideth - array.count * width - 30) / (array.count - 1);
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(15 + (width + maric) * i, 12, width, 30);
        button.layer.cornerRadius = 15;
        [button setTitle:dic[@"CateName"] forState:(UIControlStateNormal)];
        [button setTitleColor:COLORFROMCODE(0xBFBFBF, 1) forState:(UIControlStateNormal)];
        button.titleLabel.font = kFont(13);
        BUTTON_S(button, scrollViewButClick:);
        if (i == 0) {
            [button setTitleColor:COLORFROMCODE(0x2C2C2C, 1) forState:(UIControlStateNormal)];
            button.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
        }
        button.tag = 10 + i;
        [_scrollView addSubview:button];
    }
    if (_region_id) {
        NSDictionary *dic = _prodTypeArray[_buttonTag - 10];
        _fuWuType = dic[@"CateId"];
        [self getRequestDataWith:_fuWuType forPage:@""];
    }
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
}
- (void)addViwes{
    _number = 5;
    _buttonTag = 10;
    _dataDic = [NSMutableDictionary dictionary];
    _pageindex = @"1";
    _fuWuType = @"";
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 49 - 64) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = NO;
    [_tableView registerClass:[RMMHomeTableViewCell class] forCellReuseIdentifier:@"HomeCell"];
    [_tableView registerClass:[RMMHomeTableViewCell class] forCellReuseIdentifier:@"NoHomeCell"];
    [_tableView registerClass:[RMMMySkillTableViewCell class] forCellReuseIdentifier:@"MyCell"];
    [_tableView registerClass:[RMMNoSkillTableViewCell class] forCellReuseIdentifier:@"NoCell"];
    [self.view addSubview:_tableView];
    
    _scrollView = [[RMMScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 54)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.backgroundColor = kWhiteColor;
    [self addHeadScrollView];
    
    _networkView = [[RMMNotNetWorkView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    _networkView.hidden = YES;
    BUTTON_S(_networkView.button, networkBut);
    [self.view addSubview:_networkView];
    
    _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight)];
    _backImageView.image = [UIImage imageNamed:@"到店占位图"];
    _backImageView.hidden = YES;
    [self.view addSubview:_backImageView];
}
- (void)networkBut{
    [self getFuWuType];
}
// 获取我的技师
- (void)getMySkillData{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    NSString *cust_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"CUST_ID"]];
    parDic[@"cust_id"] = cust_id;
    parDic[@"org_no"] = Org_no;
    parDic[@"reg_id"] = _region_id;
    parDic[@"is_home"] = [RMMFileManager shareHandle].fuWuType;
    NSString *url = FuWuGuoDeSkillUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            [_tableView.mj_header endRefreshing];
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _skillArray = dataDic[@"data"];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    } error:^(NSError *errors) {
        [_tableView.mj_header endRefreshing];
    } view:self.view];
}
// 类型点击
- (void)scrollViewButClick:(UIButton *)button{
    _buttonTag = button.tag;
    if (_buttonTag == 10 + _prodTypeArray.count - 1) {
        if ([RMMFileManager shareHandle].isLoginSuccess) {
            [self getMySkillData];
        } else {
            [self addLoginAlertView];
        }
    } else {
        NSDictionary *dic = _prodTypeArray[button.tag - 10];
        _fuWuType = dic[@"CateId"];
        [self getRequestDataWith:_fuWuType forPage:@"1"];
        // 判读滑动
//        if (button.right <= kScreenWideth - 10) {
//            if (_scrollView.contentOffset.x != 0) {
//                [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//            }
//        } else if (button.right > kScreenWideth -  10 && button.right < 20 + 80 * _prodTypeArray.count - kScreenWideth + 20) {
//            CGFloat centrtX = kScreenWideth / 2;
//            CGFloat buttonCenterX = button.left + button.width / 2;
//            CGFloat x = buttonCenterX - centrtX;
//            [_scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
//        } else {
//            if (_scrollView.contentOffset.x != 20 + 80 * _prodTypeArray.count - kScreenWideth + 20) {
//                [_scrollView setContentOffset:CGPointMake(20 + 80 * _prodTypeArray.count - kScreenWideth, 0) animated:YES];
//            }
//        }
    }
    [_tableView reloadData];
    for (int i = 0; i < _prodTypeArray.count; i++) {
        UIButton *buttons = [_scrollView viewWithTag:10 + i];
        if (buttons.tag == _buttonTag) {
            [buttons setTitleColor:COLORFROMCODE(0x2C2C2C, 1) forState:(UIControlStateNormal)];
            buttons.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
            buttons.userInteractionEnabled = NO;
        } else {
            [buttons setTitleColor:COLORFROMCODE(0xBFBFBF, 1) forState:(UIControlStateNormal)];
            buttons.backgroundColor = kWhiteColor;
            buttons.userInteractionEnabled = YES;
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_buttonTag == 10 + _prodTypeArray.count - 1){
        if (_skillArray.count) {
            return _skillArray.count;
        } else {
            return 1;
        }
    } else {
        NSDictionary *keyDic = _prodTypeArray[_buttonTag - 10];
        NSString *key = keyDic[@"CateName"];
        if (_dataDic == nil) {
            return 0;
        }
        NSArray *array = _dataDic[key];
        if (array.count) {
            return array.count;
        } else {
            return 1;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return _scrollView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 54;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_buttonTag == 10 + _prodTypeArray.count - 1){
        if (_skillArray.count) {
            return 90;
        } else {
            return 200;
        }
    } else {
        NSDictionary *keyDic = _prodTypeArray[_buttonTag - 10];
        NSString *key = keyDic[@"CateName"];
        NSArray *array = _dataDic[key];
        if (array.count) {
            return 122 +  kScreenWideth / 2 - 15;
        } else {
            return 200;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_buttonTag == 10 + _prodTypeArray.count - 1) {
        if (_skillArray.count) {
            RMMMySkillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            BUTTON_S(cell.bookingBut, bookingButClick:);
            cell.dic = _skillArray[indexPath.row];
            cell.bookingBut.tag = indexPath.row + 1000 * _buttonTag;
            return cell;
        } else {
            RMMNoSkillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if ([[RMMFileManager shareHandle].fuWuType isEqualToString:@"1"]) {
                cell.titleLabel.text = @"您还没有预约过上门服务，暂时没有我的技师";
            } else {
                cell.titleLabel.text = @"您还没有预约过到店服务，暂时没有我的技师";
            }
            BUTTON_S(cell.bookingShangMneBut, bookingShangMneButClick);
            return cell;
        }
    } else {
        NSDictionary *keyDic = _prodTypeArray[_buttonTag - 10];
        NSString *key = keyDic[@"CateName"];
        NSArray *array = _dataDic[key];
        if (array.count) {
            RMMHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            // 这个地方会有数组越界的可能原因暂时没有找到,没有时间找了先写个判断保护下
            if (indexPath.row > array.count - 1){
                cell.model = array[0];
                cell.shopBut.tag = indexPath.row + 1000 * _buttonTag;
                BUTTON_S(cell.shopBut, addShopMart:);
            } else {
                cell.model = array[indexPath.row];
                cell.shopBut.tag = indexPath.row + 1000 * _buttonTag;
                BUTTON_S(cell.shopBut, addShopMart:);
            }
            
            return cell;
        } else {
            RMMNoSkillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoCell"];
            //cell.type = @"2";
            cell.titleLabel.text = @"门店暂时只提供面部皮肤管理服务，身体护理可选择上门服务";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
}
- (void)bookingShangMneButClick{
    
}
- (void)bookingButClick:(UIButton *)button{
    RMMOderAdressViewController *VC = [[RMMOderAdressViewController alloc] init];
    NSDictionary *dics = _skillArray[button.tag - _buttonTag * 1000];
    [RMMFileManager shareHandle].fromSkillType = @"1";
    VC.type = @"1";
    VC.isHome = [RMMFileManager shareHandle].fuWuType;
    NSDictionary *dic = @{@"EMP_NAME":dics[@"EMPLOYEE_NAME"],@"EMP_ID":dics[@"EMP_ID"]};
    [[RMMFileManager shareHandle].bookingDic setValue:dic forKey:@"技师"];
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([[RMMFileManager shareHandle].fuWuType isEqualToString:@"1"]) {
        if (_buttonTag == 10 + _prodTypeArray.count - 1) {
            if (_skillArray.count) {
                RMMSkillDeltailsViewController *VC = [[RMMSkillDeltailsViewController alloc] init];
                NSDictionary *dic = _skillArray[indexPath.row];
                VC.emp_id = dic[@"EMP_ID"];
                [self.navigationController pushViewController:VC animated:YES];
            }
        } else {
            NSDictionary *dic = _prodTypeArray[_buttonTag - 10];
            NSString *key = dic[@"CateName"];
            NSArray *value = _dataDic[key];
            RMMHomeGoodsModel *model = value[indexPath.row];
            RMMGoodsDetialsViewController *VC = [[RMMGoodsDetialsViewController alloc] init];
            VC.butTag = _buttonTag;
            if (_buttonTag == 10 || _buttonTag == 11) {
                NSString *bom_id = model.BOM_ID;
                if (bom_id.length ) {
                    RMMTaoCanDetailsViewController *deVC = [[RMMTaoCanDetailsViewController alloc] init];
                    deVC.bom_id = bom_id;
                    [self.navigationController pushViewController:deVC animated:YES];
                } else {
                    VC.service_id = model.SERVICE_ID;
                    [self.navigationController pushViewController:VC animated:YES];
                }
            } else {
                VC.service_id = model.SERVICE_ID;
                [self.navigationController pushViewController:VC animated:YES];
            }
        }
    } else {
        if (_buttonTag == 10 + _prodTypeArray.count - 1) {
            if (_skillArray.count) {
                RMMSkillDeltailsViewController *VC = [[RMMSkillDeltailsViewController alloc] init];
                NSDictionary *dic = _skillArray[indexPath.row];
                VC.emp_id = dic[@"EMP_ID"];
                [self.navigationController pushViewController:VC animated:YES];
            }
        } else {
            NSDictionary *dic = _prodTypeArray[_buttonTag - 10];
            NSString *key = dic[@"CateName"];
            NSArray *value = _dataDic[key];
            if (value.count) {
                RMMHomeGoodsModel *model = value[indexPath.row];
                RMMGoodsDetialsViewController *VC = [[RMMGoodsDetialsViewController alloc] init];
                VC.butTag = _buttonTag;
                if (_buttonTag == 10 || _buttonTag == 11) {
                    NSString *bom_id = model.BOM_ID;
                    if (bom_id.length ) {
                        RMMTaoCanDetailsViewController *deVC = [[RMMTaoCanDetailsViewController alloc] init];
                        deVC.bom_id = bom_id;
                        [self.navigationController pushViewController:deVC animated:YES];
                    } else {
                        VC.service_id = model.SERVICE_ID;
                        [self.navigationController pushViewController:VC animated:YES];
                    }
                } else {
                    VC.service_id = model.SERVICE_ID;
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
        }
    }
}
- (void)addShopMart:(UIButton *)button{
    // 首先判断有没有登陆
    if (![RMMFileManager shareHandle].isLoginSuccess) {
        // 还没有登陆
        [self addLoginAlertView];
    } else {
        if (_buttonTag == 10) {
            // 立即购买
            NSDictionary *dic = _prodTypeArray[_buttonTag - 10];
            NSString *key = dic[@"CateName"];
            NSArray *value = _dataDic[key];
            RMMHomeGoodsModel *model = value[button.tag - 1000 * _buttonTag];
            NSString *bom_id = model.BOM_ID;
            RMMTaoCanDetailsViewController *VC = [[RMMTaoCanDetailsViewController alloc] init];
            VC.bom_id = bom_id;
            [self.navigationController pushViewController:VC animated:YES];
        } else {
            [self addToShopMartWith:button.tag];
        }
    }
}
// 加入购物车
- (void)addToShopMartWith:(NSInteger)tag{
    NSString *login_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"LOGIN_ID"]];
    NSString *cust_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"CUST_ID"]];
    NSDictionary *dic = _prodTypeArray[_buttonTag - 10];
    NSString *key = dic[@"CateName"];
    NSArray *value = _dataDic[key];
    RMMHomeGoodsModel *model = value[tag - 1000 * _buttonTag];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    if (cust_id.length) {
        parDic[@"cust_login_id"] = cust_id;
        parDic[@"loginorcust"] = @"1";
    } else {
        parDic[@"cust_login_id"] = login_id;
        parDic[@"loginorcust"] = @"0";
    }
    parDic[@"serverid"] = model.SERVICE_ID;
    parDic[@"sername"] = model.NAME;
    parDic[@"count"] = @"-1";
    NSString *url = AddShopMartUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSString *backValue = [[RMMFileManager shareHandle] getValeForKey:@"SHOPMARTCOUNT"];
            NSInteger count = [backValue integerValue];
            count++;
            [[RMMFileManager shareHandle] saveVale:[NSString stringWithFormat:@"%ld",count] forKey:@"SHOPMARTCOUNT"];
            NS_POST(@"ADDSHOPMARTSUCCESS", nil, nil);
            NS_POST(@"SHOPFRESH", nil, nil);
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"添加成功!" title:nil afterDelay:1.0];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        
    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)addLoginAlertView{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要您先前往登录" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"前往" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [_tableView.mj_header endRefreshing];
        RMMLongingViewController *loginVC = [[RMMLongingViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];

    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [_tableView.mj_header endRefreshing];
        
    }];
    [alertController addAction:actionA];
    [alertController addAction:actionB];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)chooseRegion{
    [self presentViewController];
//    RMMChooseRegionViewController *VC = [[RMMChooseRegionViewController alloc] init];
//    VC.cityName = _currentCity;
//    VC.currentCity = ^void (NSString *string) {
//        [_cityBut setTitle:string forState:(UIControlStateNormal)];
//    };
//    [UIView  beginAnimations:nil context:NULL];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    [UIView setAnimationDuration:0.5];
//    [self.navigationController pushViewController:VC animated:NO];
//    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
//    [UIView commitAnimations];
    
}
- (void)setButton:(UIButton *)button withTitle:(NSString *)title{
    CGFloat width = [UILabel backHeightWithString:title CGFloatCount:kMaric metho:WIDTH font:16];
    [_cityBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -16, 0, 16)];
    [_cityBut setImageEdgeInsets:UIEdgeInsetsMake(0, width, 0, - width)];
    [_cityBut setTitle:title forState:(UIControlStateNormal)];

}
#pragma 广告------------
- (void)guangGao{
    _guangGaoView = [[RMMGuangGaoView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_guangGaoView];
    self.tabBarController.tabBar.hidden = YES;
    BUTTON_S(_guangGaoView.skipBut, skipButClick);
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:@"倒计时" repeats:YES];
    [_timer fire];
}
- (void)skipButClick{
    _guangGaoView.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    [_timer invalidate];
}
- (void)timer:(NSTimer *)timer{
    if (_number == 0) {
        _guangGaoView.hidden = YES;
        self.tabBarController.tabBar.hidden = NO;
    }
    _guangGaoView.skipLabel.text = [NSString stringWithFormat:@"跳过 %ld",_number];
    [_guangGaoView changeLabelTextColorOneWithLabel:_guangGaoView.skipLabel];
    if (_number == 0) {
        [timer invalidate];
    }
    _number--;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
